package com.xudu.xianrendongculturaltravelbackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xudu.xianrendongculturaltravelbackend.common.BaseResponse;
import com.xudu.xianrendongculturaltravelbackend.common.DeleteRequest;
import com.xudu.xianrendongculturaltravelbackend.common.ResultUtils;
import com.xudu.xianrendongculturaltravelbackend.exception.ErrorCode;
import com.xudu.xianrendongculturaltravelbackend.exception.ThrowUtils;
import com.xudu.xianrendongculturaltravelbackend.model.dto.tag.TagAddRequest;
import com.xudu.xianrendongculturaltravelbackend.model.dto.tag.TagQueryRequest;
import com.xudu.xianrendongculturaltravelbackend.model.dto.tag.TagUpdateRequest;
import com.xudu.xianrendongculturaltravelbackend.model.entity.Tag;
import com.xudu.xianrendongculturaltravelbackend.model.vo.TagVO;
import com.xudu.xianrendongculturaltravelbackend.service.TagService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;

import java.util.List;


@RestController
@RequestMapping("/tag")
public class TagController {

    @Resource
    private TagService tagService;


    /**
     * 添加标签
     */
    @PostMapping("/add")
    BaseResponse<Long> addTag(@RequestBody TagAddRequest tagAddRequest) {
        String tagName = tagAddRequest.getTagName();
        ThrowUtils.throwIf(tagName == null, ErrorCode.PARAMS_ERROR);

        Tag tag = new Tag();
        tag.setTagName(tagName);

        boolean save = tagService.save(tag);
        ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);

        return ResultUtils.success(tag.getId());
    }

    @GetMapping("/get")
    BaseResponse<Tag> getTagById(@RequestParam("id") long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        Tag tag = tagService.getById(id);
        ThrowUtils.throwIf(tag == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(tag);
    }

    @GetMapping("/list/vo")
    BaseResponse<List<TagVO>> listTag(TagQueryRequest tagQueryRequest){
        Long id = tagQueryRequest.getId();
        String tagName = tagQueryRequest.getTagName();

        QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
        if (id != null){
            tagQueryWrapper.lambda().eq(Tag::getId, id);
        }
        if (tagName != null){
            tagQueryWrapper.lambda().eq(Tag::getTagName, tagName);
        }
        tagQueryWrapper.lambda().orderByDesc(Tag::getId);
        List<Tag> tagList = tagService.list(tagQueryWrapper);

        List<TagVO> tagVOList = tagList.stream().map(Tag::objToVo).toList();

        return ResultUtils.success(tagVOList);
    }

    @PostMapping("/update")
    BaseResponse<Boolean> updateTag(@RequestBody TagUpdateRequest tagUpdateRequest) {
        Tag tag = new Tag();
        BeanUtils.copyProperties(tagUpdateRequest, tag);
        boolean update = tagService.updateById(tag);
        ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(update);
    }

    @PostMapping("/delete")
    BaseResponse<Boolean> deleteTag(@RequestBody DeleteRequest deleteRequest) {
        long id = deleteRequest.getId();
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        boolean delete = tagService.removeById(id);
        ThrowUtils.throwIf(!delete, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(delete);
    }

}